package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.File;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.police.IRegPoliceView;
import liuliu.throughwaring.control.add.police.police.RegPoliceListener;
import liuliu.throughwaring.model.PoliceModel;

/**
 * Created by liuliu on 2015/11/19   9:25
 * 注册民警信息
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class RegPoliceActivity extends BaseActivity implements IRegPoliceView {
    @CodeNote(id = R.id.reg_police_idcard)
    ImageEditText txt_idcard;//警号
    @CodeNote(id = R.id.reg_police_name)
    ImageEditText txt_name;//民警名称
    @CodeNote(id = R.id.reg_police_tel)
    ImageEditText txt_tel;//电话
    @CodeNote(id = R.id.reg_police_remark)
    ImageEditText txt_remark;//备注
    @CodeNote(id = R.id.reg_police_toolbar)
    TToolbar toolbar;
    @CodeNote(id = R.id.reg_police_reg_btn, click = "onClick")
    Button reg_btn;
    @CodeNote(id = R.id.reg_police_image)
    ImageView policeImage;
    PoliceModel policeModel;
    @CodeNote(id = R.id.reg_police_camera_btn, click = "onClick")
    Button cameraBtn;
    Bitmap bm = null;
    StringBuffer path = new StringBuffer();
    RegPoliceListener mListener;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_reg_police);
        policeModel = new PoliceModel();
        mListener = new RegPoliceListener(this, this);
    }

    @Override
    public void initEvents() {
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reg_police_reg_btn:
                if (checkPersonSave()) {
                    getText();
                    mListener.RegPolice(policeModel);
                }
                break;
            case R.id.reg_police_camera_btn:
                startCamera(11);
                break;
        }
    }

    //提取页面的数据
    private void getText() {
        policeModel.setPoliceId(txt_idcard.getText().trim());
        policeModel.setPoliceName(txt_name.getText().trim());
        policeModel.setPolicePhoneNumber(txt_tel.getText().trim());
        policeModel.setPoliceComment(txt_remark.getText().trim());
        policeModel.setPoliceCardImage(path.toString());
//        policeModel.setPoliceCardImage(Utils.encodeBitmap((Bitmap) policeImage.getTag()));
    }

    //开启拍照
    public void startCamera(int type) {
        // 利用系统自带的相机应用:拍照
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        path = new StringBuffer();
        path.append(RegPoliceActivity.this.getExternalFilesDir(null)).append("/" + Utils.getImgUrlByTime() + ".jpg");
        File file = new File(path.toString());
        Uri uri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, type);
    }

    private boolean checkPersonSave() {
        if (txt_idcard.getText().equals("")) {
            mUtils.ToastShort("民警编号不能为空");
            return false;
        } else if (txt_name.getText().equals("")) {
            mUtils.ToastShort("姓名不能为空");
            return false;
        } else if (txt_tel.getText().equals("")) {
            mUtils.ToastShort("电话不能为空");
            return false;
        } else {
            return true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == -1) {
            if (requestCode == 11) {
                bm = Utils.getimage(this, path.toString());
                if (bm != null) {
                    policeImage.setImageBitmap(Utils.compressImage(bm));
                    policeImage.setTag(bm);
                } else {
                    policeImage.setImageResource(R.mipmap.person_header);
                    policeImage.setTag(bm);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRegResult(boolean isTrue, String mes) {
        if (isTrue) {
            this.setResult(133, new Intent());//返回页面1
            RegPoliceActivity.this.finish();
            Utils.ToastShort(RegPoliceActivity.this, "添加成功");
        } else {
            Utils.ToastShort(RegPoliceActivity.this, mes);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bm != null) {
            if (!bm.isRecycled()) {
                bm.recycle();//回收图片所占的内存
                System.gc();
            }
        }
    }
}
